<template>
  <div id="app">
    <van-dialog v-model="show" :showConfirmButton="false" style="width: 84.5%">
      <van-icon
        @click="hide"
        size="20"
        name="cross"
        style="position: absolute; top: 15px; right: 15px"
      />
      <img width="100%" :src="qrcode" />
    </van-dialog>
    <keep-alive>
      <router-view
        class="view-router"
        v-if="$route.meta.keepAlive"
      ></router-view>
    </keep-alive>
    <router-view
      class="view-router"
      v-if="!$route.meta.keepAlive"
    ></router-view>
    <router-view name="tabbar"></router-view>
  </div>
</template>
<script>
import { authInfo, geocoderV } from '@/api/api';
import { setLocalStorage, getLocalStorage } from '@/utils/local-storage';
import { Dialog, Divider, Button, Icon, Toast } from 'vant';
import wxShare from '@/utils/wxShare';
import remConfig from '@/utils/rem';
import wx from 'weixin-js-sdk';
export default {
  data() {
    return {
      timer: '',
      userId: -1,
      mobile: '',
      include: [],
      directorId: '',
      isAttention: '',
      inShareId: null,
      yashi: 0,
      setime: 10,
      print: true,
      userLevel: '',
      show: false,
      ctlShow: false, // 控制弹窗是否显示
      //qrcode: 'https://xintu361.oss-cn-qingdao.aliyuncs.com/p53lmnwxe4gvlt145cqv.png'
      qrcode:
        'https://santu-dev.oss-cn-shanghai.aliyuncs.com/akprfx79475h659i4m89.png',
      area: '',
    };
  },
  created() {
    this.initAload();
    remConfig();
    authInfo()
      .then((res) => {
        this.userId = res.data.data.userId;
        this.mobile = res.data.data.mobile || '';
        this.isAttention = res.data.data.isAttention;
        this.directorId = res.data.data.directorId;
        this.userLevel = res.data.data.userLevel;
        if (this.isAttention == 1) {
          this.ctlShow = false;
          this.show = false;
        } else {
          // 0,已取关 2 未关注 1 已关注
          if (this.isAttention != 1) {
            // 如果未关注，并且ctlShow不显示，则重新显示
            this.ctlShow = true;
            this.checkUser();
          }
        }

        setLocalStorage({
          userId: this.userId,
          mobile: this.mobile,
          directorId: this.directorId,
          userLevel: this.userLevel,
        });
      })
      .catch((e) => {
        if (this.isAttention == 1) {
          this.ctlShow = false;
          this.show = false;
        } else {
          // 0,已取关 2 未关注 1 已关注
          if (this.isAttention != 1) {
            // 如果未关注，并且ctlShow不显示，则重新显示
            this.ctlShow = true;
            this.checkUser();
          }
        }
      });
    this.setShareId();
  },
  beforeDestroy() {
    //清除定时器
    // clearInterval(this.timer);
  },

  watch: {
    // 监听 $route 变化调用分享链接
    $route(to, from) {
      // console.log(to, 9);

      if (
        from.name == 'project-detail' ||
        from.name == 'workers-detail' ||
        from.name == 'contract/detail'
      ) {
        if (from.meta.deepth > to.meta.deepth) {
          to.meta.keepAlive = true;
          console.log(to.meta.keepAlive, 1);
        }
      } else if (
        to.name == 'project' ||
        to.name == 'workers' ||
        to.name == 'contract'
      ) {
        to.meta.keepAlive = false;
        console.log(to.meta.keepAlive, 0);
      }
      // console.log(to, 'to', from, 'from');
      if (to.name == 'orderCheckout' && from.name == 'payment') {
        this.$router.replace({ path: '/user/order/list/1' });
      }
      if (
        to.fullPath == '/check' &&
        to.fullPath == '/verify/user' &&
        to.fullPath == '/verify/invite'
      ) {
        this.print = false;
      } else if (to.fullPath != '/check') {
        this.print = true;
      }
      if (
        this.inShareId != null &&
        from.fullPath == '/' &&
        to.fullPath != '/'
      ) {
        this.inShareId = null;
        this.$router.replace({ path: '/' }, () => {
          this.$router.push({ path: to.fullPath });
        });
      }
      wxShare({
        title: to.meta.title,
        desc: to.meta.title,
        link: to.fullPath,
        imgUrl:
          'https://xintu361.oss-cn-qingdao.aliyuncs.com/sbhvjsrmjusefo547b0b.jpg',
      });
    },
  },

  methods: {
    setShareId() {
      // removeLocalStorage('shareId');
      // 获取分享用户ID
      let name,
        value,
        count,
        str = window.location.href,
        num = str.indexOf('?'); //取得整个地址栏
      str = str.substr(num + 1); //取得所有参数 stringvar.substr(start [, length ] substr()是抽取对应下标的字符
      let arr = str.split('&');
      //各个参数放到数组里

      for (let i = 0; i < arr.length; i++) {
        count = arr[i].indexOf('='); //indexOf返回-1时表示为空,当有多个值时 会返回多个对应的数据

        setLocalStorage({
          pnaduan: count,
        });
        if (count > 0) {
          name = arr[i].substring(0, count); //substring和subtr一样 都是截取用，参数不一样时才有所不同

          value = arr[i].substr(count + 1);

          if (name == 'shareId') {
            setLocalStorage({
              shareId: value,
            });

            this.inShareId = value;
          }
          if (name == 'userId') {
            setLocalStorage({
              getuserId: value,
            });
          }
        }
      }
    },
    hide() {
      this.show = false;
      this.checkUser();
    },
    checkUser() {
      setTimeout(() => {
        if (this.ctlShow && this.print) {
          //this.show = true;
        }
        if (this.show) {
          this.yashi++;
        }
        if (this.yashi == 1) {
          this.setime = 30;
        }
        if (this.yashi == 2) {
          this.setime = 40;
        }
        if (this.yashi == 3) {
          this.setime = 36000;
        }

        // if (this.yashi == 4) {
        //   this.yashi = 0;
        //   this.show = false;
        //   this.ctlShow = false;
        // }
      }, 1000 * this.setime);
    },
    getAuthInfo: function () {
      // authInfo()
      //   .then((res) => {
      //     this.userId = res.data.data.userId;
      //     this.mobile = res.data.data.mobile || '';
      //     this.isAttention = res.data.data.isAttention;
      //     this.checkUser();
      //     setLocalStorage({
      //       userId: this.userId,
      //       mobile: this.mobile,
      //     });
      //   })
      //   .catch((e) => {
      //     console.log(e);
      //   });
    },
    initAload() {
      this.watchAreatState();
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          localStorage.setItem('latitude', res.latitude);
          localStorage.setItem('longitude', res.longitude);
        },
      });
    },
    watchAreatState() {
      let latitude = localStorage.getItem('latitude');
      let longitude = localStorage.getItem('longitude');
      let dataLat = latitude + ',' + longitude;
      if (latitude == null) {
        dataLat = null;
      }
      if (
        localStorage.getItem('areacode') != null &&
        localStorage.getItem('areacode') != ''
      ) {
        let arealocal = {
          code: localStorage.getItem('areacode'),
          city: localStorage.getItem('areacity'),
        };
        this.$store.state.area = arealocal;
      }
      geocoderV({ str: dataLat }).then((resd) => {
        let vresd = resd.data.data;
        let area = {
          code: vresd.ad_info.adcode.toString(),
          city: vresd.ad_info.city,
        };
        localStorage.setItem('areacode', area.code);
        localStorage.setItem('areacity', area.city);
        if (area.code != null && area.code != '') {
          this.area = area;
          this.$store.state.area = this.area;
        }
      });
    },
  },
  components: {
    [Dialog.Component.name]: Dialog.Component,
    [Divider.name]: Divider,
    [Button.name]: Button,
    [Icon.name]: Icon,
  },
};
</script>
<style lang="scss" src="./assets/scss/global.scss" />
